JavaScript Mistakes

# JavaScript Common Mistakes

« Previous
Next Chapter »

This chapter points at some common JavaScript mistakes.

## Accidentally Using the Assignment Operator

This statement only returns true, if x1 equals 10:

if (x1 == 10)
Try it yourself »

This statement always returns true, because the assignment is always true:

if (x1 = 10)
Try it yourself »

## Confusing Addition & Concatenation

Addition is about adding numbers.

Concatenation is about adding strings.

In JavaScript both operations use the same + operator.

Because of this, when adding a number as a number, will produce a different result from adding a number as a string:

var x = 10 + 5; // the result in x is 15
var x = 10 + "5"; // the result in x is "105"
Try it yourself »

When adding two variables, it can be difficult to anticipate the result:

var x = 10;
var y = 5;
var z = x + y; // the result in z is 15
var x = 10;
var y = "5";
var z = x + y; // the result in z is "105"
Try it yourself »

## Misunderstanding Floats

All numbers in JavaScript are stored as 64-bits Floating point numbers (Floats).

All programming languages, including JavaScript, have difficulties with precise floating point values:

var x = 0.1;
var y = 0.2;
var z = x + y // the result in z will not be 0.3
if (z == 0.3) // this if test will fail
Try it yourself »

## Breaking a JavaScript String

JavaScript will allow you to break a statement into two lines:

## Example 1

var x =
"Hello World!";
Try it yourself »

But, breaking a statement in the middle of a string will not work:

## Example 2

var x = "Hello
World!";
Try it yourself »

You must use a "backslash" if you must break a statement in a string:

## Example 3

var x = "Hello \
World!";
Try it yourself »

## Misplacing Semicolon

Because of a misplaced semicolon, this code block will execute regardless of the value of x:

if (x == 19);
{
// code block
}
Try it yourself »

## Breaking a Return Statement

It is a default JavaScript behavior to close a statement automatically at the end of a line.

Because of this, these two examples will return the same result:

## Example 1

function myFunction(a) {
var power = 10
return a * power
}
Try it yourself »

## Example 2

function myFunction(a) {
var power = 10;
return a * power;
}
Try it yourself »

JavaScript will also allow you to break a statement into two lines.

Because of this, example 3 will also return the same result:

## Example 3

function myFunction(a) {
var
power = 10;
return a * power;
}
Try it yourself »

But, what will happen if you break the return statement in two lines like this:

## Example 4

function myFunction(a) {
var
power = 10;
return
a * power;
}
Try it yourself »

The function will return undefined!

Why? Because JavaScript thinks you meant:

## Example 5

function myFunction(a) {
var
power = 10;
return;
a * power;
}
Try it yourself »

## Explanation

If a statement is incomplete like:

var

JavaScript will try to complete the statement by reading the next line:

power = 10;

But since this statement is complete:

return

JavaScript will automatically close it like this:

return;

This happens because closing (ending) statements with semicolon is optional in JavaScript.

JavaScript will close the return statement at the end of the line, because it is a complete statement.

| Never break a return statement.

## Accessing Arrays with Named Indexes

Many programming languages support arrays with named indexes.

Arrays with named indexes are called associative arrays (or hashes).

JavaScript does not support arrays with named indexes.

In JavaScript, arrays use numbered indexes:

## Example:

var person = [];
person[0] = "John";
person[1] = "Doe";
person[2] = 46;
var x = person.length; // person.length will return 3
var y = person[0]; // person[0] will return "John"
Try it Yourself »

In JavaScript, objects use named indexes.

If you use a named index, when accessing an array, JavaScript will redefine the array to a standard object.

After the automatic redefinition, array methods and properties will produce undefined or incorrect results:

## Example:

var person = [];
person["firstName"] = "John";
person["lastName"] = "Doe";
person["age"] = 46;
var x = person.length; // person.length will return 0
var y = person[0]; // person[0] will return undefined
Try it Yourself »

## Ending an Array Definition with a Comma

## Incorrect:

points = [40, 100, 1, 5, 25, 10,];

Some JSON and JavaScript engines will fail, or behave unexpectedly.

## Correct:

points = [40, 100, 1, 5, 25, 10];

## Ending an Object Definition with a Comma

## Incorrect:

person = {firstName:"John", lastName:"Doe", age:46,}

Some JSON and JavaScript engines will fail, or behave unexpectedly.

## Correct:

person = {firstName:"John", lastName:"Doe", age:46}

## Undefined is Not Null

With JavaScript, null is for objects, undefined is for variables, properties, and methods.

To be null, an object has to be defined, otherwise it will be undefined.

If you want to test if an object exists, this will throw an error if the object is undefined:

## Incorrect:

if (myObj !== null && typeof myObj !== "undefined")

Because of this, you must test typeof() first:

## Correct:

if (typeof myObj !== "undefined" && myObj !== null)

## Expecting Block Level Scope

JavaScript does not create a new scope for each code block.

It is true in many programming languages, but not true in JavaScript.

It is a common mistake, among new JavaScript developers, to believe that this code returns undefined:

## Example:

for (var i = 0; i < 10; i++) {
// some code
}
return i;
Try it Yourself »
« Previous
Next Chapter »

### WEB HOSTING

UK Reseller Hosting

### WEB BUILDING

FREE Website BUILDER Free HTML5 Templates

### W3SCHOOLS EXAMS

HTML, CSS, JavaScript, PHP, jQuery, XML, and ASP Certifications

### SHARE THIS PAGE

### Color Picker

### HTML/CSS

HTML Tutorial HTML5 Tutorial CSS Tutorial CSS3 Tutorial Bootstrap Tutorial

### JavaScript

JavaScript Tutorial jQuery Tutorial jQuery Mobile Tutorial AngularJS Tutorial AJAX Tutorial JSON Tutorial

### Graphics

Canvas Tutorial SVG Tutorial Google Maps Tutorial

### Server Side

SQL Tutorial PHP Tutorial ASP Tutorial ASP.NET Tutorial VBScript Tutorial AppML Tutorial

### Web

Web Building Web Statistics Web Certification

### XML

XML Tutorial DTD Tutorial Schema Tutorial XML DOM Tutorial XPath Tutorial XSLT Tutorial XQuery Tutorial XSL-FO Tutorial RSS Tutorial WSDL Tutorial

### HTML/CSS

HTML Tag Reference HTML Event Reference HTML Color Reference CSS 1,2,3 Reference Bootstrap Reference

### JavaScript

JavaScript Reference HTML DOM Reference jQuery Reference jQuery Mobile Reference Google Maps Reference

### Server Side

PHP Reference SQL Reference ASP Reference VBScript Reference Razor Reference ASP.NET Reference

### XML

XML DOM Reference XSLT Reference XSL-FO Reference XPath Reference XQuery Reference Schema Reference RSS Reference SVG Reference

### HTML Charsets

HTML Character Sets HTML ASCII HTML ANSI HTML Windows-1252 HTML ISO-8859-1 HTML Symbols HTML UTF-8

### HTML/CSS

HTML Examples CSS Examples

### Browser Scripting

JavaScript Examples HTML DOM Examples jQuery Examples jQuery Mobile Examples AngularJS Examples AJAX Examples

### Server Scripting

PHP Examples ASP Examples VBScript Examples Razor Examples .NET Examples

### XML Languages

XML Examples DTD Examples XML DOM Examples XSL Examples XSLT Examples XPath Examples XQuery Examples Schema Examples RSS Examples SVG Examples